import React from "react";

const Comment = () => {
    const back = () => {
        React.navigate(-1)
    }

    React.useEffect(() => {
        getOrderData()
    }, [])

    // 获取传来的参数
    const [useSearchParams] = React.Router.useSearchParams();
    const orderid = useSearchParams.get("orderid")

    // 获得订单数据
    // 获取订单数据
    const [order, setOrder] = React.useState({
        comment:"",
        rate:"",
        room: {
            name: "",
        }
    })

    const [form] = React.UI.Form.useForm()
    const [formData,setFormData] = React.useState({
        comment:"",
        rate:"",
        name:"",
    })
    const getOrderData = async () => {
        let result = await React.HTTP.post("/order/info", { orderid })
        if (result.code == 1) {
            setOrder(result.data.order)
            form.setFieldValue("name",result.data.order.room.name)
        } else {
            React.error(result.msg, () => {
                React.navigate(-1)
            })
        }
    }

    // 发表评论
    const onfinish = async (val:any) =>{
        val.orderid = orderid
        let result = await React.HTTP.post("/order/comment",val)
        if(result.code == 1){
            React.success(result.msg,()=>{
                React.navigate(-1)
            })
        }else{
            React.error(result.msg)
            return false
        }
    }

    return (<>
        <React.UI.NavBar
            onBack={back}
            style={{ background: `rgb(55, 68, 134)`, color: '#fff' }}
        >发表评论</React.UI.NavBar>

        <React.UI.Form
            form={form}
            layout='horizontal'
            initialValues={formData}
            onFinish={onfinish}
            footer={
                <React.UI.Button block type='submit' color='primary' size='large'>
                    提交
                </React.UI.Button>
            }
        >
            <React.UI.Form.Item
                name='name'
                label='房间名称'
                rules={[{ re下quired: true, message: '房间名称不能为空' }]}
            >
                <React.UI.Input readOnly placeholder='请输入姓名' />
            </React.UI.Form.Item>
            <React.UI.Form.Item
                name='rate'
                label='评分'
                rules={[{ required: true, message: '评分不能为空' }]}
            >
                <React.UI.Rate allowHalf />
            </React.UI.Form.Item>
            <React.UI.Form.Item
                name='comment'
                label='评论'
                rules={[{ required: true, message: '评论不能为空' }]}
            >
                <React.UI.TextArea
                    placeholder='请输入评论'
                />
            </React.UI.Form.Item>
        </React.UI.Form>
    </>)
}

export default Comment